
package com.lzb.permanentcalendar.dialog;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.aigestudio.wheelpicker.WheelPicker;
import com.lzb.permanentcalendar.R;
import com.lzb.permanentcalendar.basic.dialog.BaseSmartDialog;
import com.lzb.permanentcalendar.databinding.DialogSolarDateBinding;
import com.nlf.calendar.Solar;
import com.nlf.calendar.util.SolarUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SolarDateDialog extends BaseSmartDialog<DialogSolarDateBinding> implements View.OnClickListener {

    private List<String> years = new ArrayList<>();
    private List<String> mos = new ArrayList<>();
    private List<String> mos1 = new ArrayList<>();
    private List<String> days = new ArrayList<>();
    private List<String> days1 = new ArrayList<>();

    public SolarDateDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_solar_date;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.tvCancel.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);

        mDataBinding.wheelPicker1.setItemTextSize(50);
        mDataBinding.wheelPicker2.setItemTextSize(50);
        mDataBinding.wheelPicker3.setItemTextSize(50);
        mDataBinding.wheelPicker1.setItemTextColor(Color.parseColor("#80202739"));
        mDataBinding.wheelPicker2.setItemTextColor(Color.parseColor("#80202739"));
        mDataBinding.wheelPicker3.setItemTextColor(Color.parseColor("#80202739"));
        mDataBinding.wheelPicker1.setSelectedItemTextColor(Color.parseColor("#202739"));
        mDataBinding.wheelPicker2.setSelectedItemTextColor(Color.parseColor("#202739"));
        mDataBinding.wheelPicker3.setSelectedItemTextColor(Color.parseColor("#202739"));
        mDataBinding.wheelPicker1.setVisibleItemCount(5);
        mDataBinding.wheelPicker2.setVisibleItemCount(5);
        mDataBinding.wheelPicker3.setVisibleItemCount(5);
        mDataBinding.wheelPicker1.setItemSpace(80);
        mDataBinding.wheelPicker2.setItemSpace(80);
        mDataBinding.wheelPicker3.setItemSpace(80);
        setData();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            dismiss();
        } else if (id == R.id.tvConfirm) {
            dismiss();
            if (listener != null) {
                int selectedItemPositionYear = mDataBinding.wheelPicker1.getCurrentItemPosition();
                String yearStr = (String) mDataBinding.wheelPicker1.getData().get(selectedItemPositionYear);
                int year = Integer.parseInt(yearStr.toString().replace("年", ""));
                int selectedItemPosition = mDataBinding.wheelPicker2.getCurrentItemPosition();
                String moS = (String) mDataBinding.wheelPicker2.getData().get(selectedItemPosition);
                int mo = Integer.parseInt(moS.toString().replace("月", ""));
                int selectedItemPositionDay = mDataBinding.wheelPicker3.getCurrentItemPosition();
                String dayStr = (String) mDataBinding.wheelPicker3.getData().get(selectedItemPositionDay);
                int day = Integer.parseInt(dayStr.toString().replace("日", ""));
                listener.clickConfirm(year, mo, day);
            }
        }
    }

    private void setData() {
        years.clear();
        Solar solar = Solar.fromDate(new Date());
        int yearIndex = 0;
        for (int i = 1900; i <= 2100; i++) {
            years.add(i + "年");
        }
        for (int i = 0; i < years.size(); i++) {
            if (years.get(i).equals(solar.getYear() + "年")) {
                yearIndex = i;
            }
        }
        mDataBinding.wheelPicker1.setData(years);
        int finalYearIndex = yearIndex;
        mDataBinding.wheelPicker1.post(new Runnable() {
            @Override
            public void run() {
                mDataBinding.wheelPicker1.setSelectedItemPosition(finalYearIndex);
            }
        });
        mos.clear();
        days.clear();
        for (int i = 1; i <= 12; i++) {
            mos.add(i + "月");
        }
        int moIndex = 0;
        for (int i = 0; i < mos.size(); i++) {
            if (mos.get(i).equals(solar.getMonth() + "月")) {
                moIndex = i;
            }
        }
        mDataBinding.wheelPicker2.setData(mos);
        int daysOfMonth = SolarUtil.getDaysOfMonth(solar.getYear(), solar.getMonth());
        setDays(daysOfMonth);
        int dayIndex = 0;
        for (int i = 0; i < days.size(); i++) {
            if (days.get(i).equals(solar.getDay() + "日")) {
                dayIndex = i;
            }
        }
        int finalMoIndex = moIndex;
        mDataBinding.wheelPicker2.post(new Runnable() {
            @Override
            public void run() {
                mDataBinding.wheelPicker2.setSelectedItemPosition(finalMoIndex);
            }
        });
        int finalDayIndex = dayIndex;
        mDataBinding.wheelPicker3.post(new Runnable() {
            @Override
            public void run() {
                mDataBinding.wheelPicker3.setSelectedItemPosition(finalDayIndex);
            }
        });
        mDataBinding.wheelPicker1.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                days.clear();
                if (data.toString().contains("年")) {
                    int year = Integer.parseInt(data.toString().replace("年", ""));
                    int selectedItemPosition = mDataBinding.wheelPicker2.getCurrentItemPosition();
                    String moS = (String) mDataBinding.wheelPicker2.getData().get(selectedItemPosition);
                    int mo = Integer.parseInt(moS.toString().replace("月", ""));
                    int daysOfMonth = SolarUtil.getDaysOfMonth(year, mo);
                    setDays(daysOfMonth);
                }
            }
        });
        mDataBinding.wheelPicker2.setOnItemSelectedListener(new WheelPicker.OnItemSelectedListener() {
            @Override
            public void onItemSelected(WheelPicker picker, Object data, int position) {
                if (data.toString().contains("月")) {
                    int mo = Integer.parseInt(data.toString().toString().replace("月", ""));
                    int selectedItemPosition = mDataBinding.wheelPicker1.getCurrentItemPosition();
                    String yearStr = (String) mDataBinding.wheelPicker1.getData().get(selectedItemPosition);
                    int year = Integer.parseInt(yearStr.toString().replace("年", ""));
                    int daysOfMonth = SolarUtil.getDaysOfMonth(year, mo);
                    setDays(daysOfMonth);
                }
            }
        });

    }

    private void setDays(int daysOfMonth) {
        days.clear();
        for (int i = 1; i <= daysOfMonth; i++) {
            days.add(i + "日");
        }
        mDataBinding.wheelPicker3.setData(days);
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConfirm(int year, int mo, int days);
    }
}
